/*
 * @Author: jackning 270580156@qq.com
 * @Date: 2024-01-29 16:21:46
 * @LastEditors: jackning 270580156@qq.com
 * @LastEditTime: 2024-04-15 16:30:27
 * @Description: bytedesk.com https://github.com/Bytedesk/bytedesk
 *   Please be aware of the BSL license restrictions before installing Bytedesk IM – 
 *  selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license. 
 *  仅支持企业内部员工自用，严禁私自用于销售、二次销售或者部署SaaS方式销售 
 *  Business Source License 1.1: https://github.com/Bytedesk/bytedesk/blob/main/LICENSE 
 *  contact: 270580156@qq.com 
 *  联系：270580156@qq.com
 * Copyright (c) 2024 by bytedesk.com, All Rights Reserved. 
 */
package com.bytedesk.core.socket.mqtt.protocol;

import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.*;
// import com.bytedesk.core.socket.mqtt.model.MqttDupPubRelMessage;
// import com.bytedesk.core.socket.mqtt.service.MqttDupPubRelMessageStoreService;
// import com.bytedesk.core.socket.mqtt.service.MqttDupPublishMessageStoreService;
import com.bytedesk.core.socket.mqtt.util.ChannelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
public class PubRec {

        // private final MqttDupPublishMessageStoreService
        // mqttDupPublishMessageStoreService;

        // private final MqttDupPubRelMessageStoreService
        // mqttDupPubRelMessageStoreService;

        public void processPubRec(Channel channel, MqttMessageIdVariableHeader variableHeader) {
                //
                int messageId = variableHeader.messageId();
                String clientId = ChannelUtils.getClientId(channel);
                log.debug("PUBREC - clientId: {}, messageId: {}", clientId, messageId);
                //
                // mqttDupPublishMessageStoreService.remove(clientId, messageId);
                //
                // MqttDupPubRelMessage dupPubRelMessageStore = new
                // MqttDupPubRelMessage().setClientId(clientId).setMessageId(messageId);
                // mqttDupPubRelMessageStoreService.put(clientId, dupPubRelMessageStore);
                //
                MqttMessage pubRelMessage = MqttMessageFactory.newMessage(
                                new MqttFixedHeader(MqttMessageType.PUBREL,
                                                false,
                                                MqttQoS.AT_MOST_ONCE,
                                                false,
                                                0),
                                MqttMessageIdVariableHeader.from(
                                                messageId),
                                null);
                channel.writeAndFlush(pubRelMessage);
        }
}
